package com.ftg.learn.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 这是一个通用类,用来完成mybatis plus通用controller的封装
 * @author Administrator
 */
public class BaseController<E , T extends IService<E> > {

    //service 需要
    //其实是一个接口
    @Autowired  //spring -- bytype 从容器找到当前类或者接口的 实现现 实例给当前变量
    public T service;

    /**
     * 通用查询全部方法
     * @return
     */
    @GetMapping(value="/showAll")
    public ResEntity showAll(){
        return new ResEntity(0,"success",service.list());
    }

    @GetMapping("/showByPara")
    public ResEntity showAll(String column,String val){
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(column,val);
        List<E> list = service.list(queryWrapper);
        return new ResEntity(0,"success",list);
    }

    @GetMapping("/showByParas")
    public ResEntity showAll(String[] column,String[] val){
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        for (int i =0,len = column.length;i < len;i++) {
            queryWrapper.like(column[i],val[i]);
        }
        List<E> list = service.list(queryWrapper);
        return new ResEntity(0,"success",list);
    }

    @GetMapping("/oneById/{id}")
    public ResEntity one(@PathVariable Integer id){
        E one = service.getById(id);
        return new ResEntity(0,"success",one);
    }

    @GetMapping("/oneByPara/{column}/{val}")
    public ResEntity one(@PathVariable String column,@PathVariable String val){
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column,val);
        E one = service.getOne(queryWrapper);
        return new ResEntity(0,"success",one);
    }


    /**
     * 通用查询全部带分页方法
     * @param page  当前页码
     * @param limit  每页显示多少条
     * @return  ResPageEntity
     */
    @GetMapping("/page")
    public ResPageEntity page(@RequestParam int page,@RequestParam int limit){
        IPage<E> ip = new Page<>(page,limit);
        service.page(ip);
        return new ResPageEntity(ip);
    }

    /**
     * 通用查询全部带分页方法
     * @param page
     * @param limit
     * @return ResPageEntity
     */
    @PostMapping("/pageByParam")
    public ResPageEntity page(@RequestParam String column,@RequestParam String val,@RequestParam int page,@RequestParam int limit){
        IPage<E> ip = new Page<>(page,limit);
        QueryWrapper<E> query = new QueryWrapper<>();
        query.like(column,val);
        service.page(ip,query);
        return new ResPageEntity(ip);
    }

    @PostMapping("/saveOrUpdate")
    public ResEntity saveOrUpdate(@RequestBody E e){
        ResEntity resEntity;
        if (service.saveOrUpdate(e)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

    @PostMapping("/save")
    public ResEntity save(@RequestBody E e){
        System.out.println("我执行添加方法了");
        System.out.println(e);
        ResEntity resEntity;
        if (service.save(e)) {
            System.out.println("if里面");
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

    @PutMapping("/update")
    public ResEntity update(@RequestBody E e){
        ResEntity resEntity;
        if (service.updateById(e)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }


    @DeleteMapping("/del/{id}")
    public ResEntity delete(@PathVariable Serializable id){
        ResEntity resEntity;
        if (service.removeById(id)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

    @DeleteMapping("/delids")
    public ResEntity deleteIds(@RequestParam List<Serializable> ids){
        ResEntity resEntity;
        if ( service.removeByIds(ids)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

}
